package com.ahdms.es.gm.asn1.generic;

import org.bouncycastle.asn1.*;
import org.bouncycastle.asn1.cmp.CMPCertificate;

/**
 * @author zhoumin
 * @version 1.0.0
 * @date 2021/3/16 8:47
 */
public class PKCS12 extends ASN1Object {

    private DERBitString ske;
    private CMPCertificate cert;

    public static PKCS12 getInstance(Object o) {
        if (o instanceof PKCS12) {
            return (PKCS12) o;
        } else if (o instanceof ASN1Sequence) {
            return new PKCS12((ASN1Sequence) o);
        } else if (o instanceof byte[]) {
            return new PKCS12(ASN1Sequence.getInstance(o));
        }
        return null;
    }

    public PKCS12(DERBitString ske, CMPCertificate cert) {
        this.ske = ske;
        this.cert = cert;
    }

    protected PKCS12(ASN1Sequence sequence) {
        this.ske = DERBitString.getInstance(sequence.getObjectAt(0));
        this.cert = CMPCertificate.getInstance(sequence.getObjectAt(1));
    }

    public DERBitString getSke() {
        return ske;
    }

    public CMPCertificate getCert() {
        return cert;
    }

    @Override
    public ASN1Primitive toASN1Primitive() {
        ASN1EncodableVector v = new ASN1EncodableVector();
        v.add(ske);
        v.add(cert);
        return new DERSequence(v);
    }
}
